package com.hrbxlh.action;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.hrbxlh.actionImpl.TeaStatisticsActionImpl;
import com.hrbxlh.util.PubMethods;
import com.hrbxlh.util.SessionValues;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * -------------------------------------------------------
 * Copyright (c) 2017, 鑫联华信息科技股份有限公司
 * All rights reserved.
 * FileName：TeaStatisticsAction.java
 * Description：教师统计action类
 * History：
 * Date           Author               Desc
 * 2018.2.23		  sy                   建立
 * -------------------------------------------------------
 */

@Controller
public class TeaStatisticsAction {
    @Autowired
    @Qualifier("teaStatisticsActionImpl")
    private TeaStatisticsActionImpl teaStatisticsActionImpl;

    //日志信息
    Logger logger = Logger.getLogger(TeaStatisticsAction.class.getName());
    /**
     * param:classEndMark:0未结课、1已结课、3全部  默认给3
     * param:pageNow、pageSize
     * param:搜素条件：课程id courseId
     * @return 课程统计 ：(按课程统计已学人数(已结课的)、正在学习人数（未结课的）这两个按搜素条件查询)、问题交流问题数、回答问题数、实验学习总时长、实验学习平均实长、
     * 平时成绩平均成绩、考试成绩平均成绩、总平均成绩平均成绩、考试及格率、实验报告提交率、作业提交率、课程id、课程名。
     * @throws IOException
     */
    @SuppressWarnings("unchecked")
    @RequestMapping(value = "/getCouStatisticsByTeaId", method = RequestMethod.POST)
    public void getCouStatisticsByTeaId(String jsonStr,HttpServletRequest request, HttpServletResponse response) throws IOException {

        //转换json格式工具
        ObjectMapper mapper = new ObjectMapper();
        //存放结果值
        Map<String,Object> resultMap = new HashMap<String,Object>();
        try {
            //获取参数值
            Map<String,Object> paramMap = mapper.readValue(jsonStr, resultMap.getClass());
            //课程统计
            Map<String,Object> couStatisticsMap = teaStatisticsActionImpl.getCouStatisticsByTeaId(paramMap,request);
            resultMap.put("couStatisticsMap",couStatisticsMap);
            resultMap.put("result", "success");
            resultMap.put("failMsg", "");
        } catch (Exception e) {
            resultMap.put("result", "fail");
            resultMap.put("failMsg", "跳转页面失败");
            e.printStackTrace();
            logger.error("按课程统计出现异常", e);

        } finally {
            logger.info("按课程统计的返回结果为："+resultMap);
            PubMethods.postMed(response,mapper,resultMap);
        }
    }

    /**
     * 课程详情
     * param:classEndMark:0未结课、1已结课、3全部  默认给3、课程id courseId
     * param:搜素条件：课程id courseId
     * @return 课程统计 ：(按课程统计已学人数(已结课的)、正在学习人数（未结课的）这两个按搜素条件查询)、问题交流问题数、回答问题数、实验学习总时长、实验学习平均实长、
     * 平时成绩平均成绩、考试成绩平均成绩、总平均成绩平均成绩、考试及格率、实验报告提交率、作业提交率、课程id、课程名。
     * @throws IOException
     */
    @SuppressWarnings("unchecked")
    @RequestMapping(value = "/getCouStatisticsByTeaIdDetail", method = RequestMethod.POST)
    public void getCouStatisticsByTeaIdDetail(String jsonStr,HttpServletRequest request, HttpServletResponse response) throws IOException {

        //转换json格式工具
        ObjectMapper mapper = new ObjectMapper();
        //存放结果值
        Map<String,Object> resultMap = new HashMap<String,Object>();
        try {
            //获取参数值
            Map<String,Object> paramMap = mapper.readValue(jsonStr, resultMap.getClass());
            //课程统计
            Map<String,Object> couStatisticsDetailMap = teaStatisticsActionImpl.getCouStatisticsByTeaIdDetail(paramMap,request);
            resultMap.put("couStatisticsDetailMap",couStatisticsDetailMap);
            resultMap.put("result", "success");
            resultMap.put("failMsg", "");
        } catch (Exception e) {
            resultMap.put("result", "fail");
            resultMap.put("failMsg", "跳转页面失败");
            e.printStackTrace();
            logger.error(" 获取课程详情出现异常", e);

        } finally {
            logger.info("获取课程详情的返回结果为："+resultMap);
            PubMethods.postMed(response,mapper,resultMap);
        }
    }

    /**
     * 学生统计
     * param:教师id
     * param:搜素条件：班级id classId
     * @return 统计学生的实验总时间、提问问题数、回答问题数、提交作业数、实验报告数。实验笔记数、平时成绩、考试成绩、总成绩、学生姓名、
     * 课程名。点击详情显示每门课程的实验时长、课程成绩分数、当前课程排名。每个章节的课程成绩（平时成绩）、班级平均成绩、实验时长、该实验章节平均时长。
     * @throws IOException
     */
    @SuppressWarnings("unchecked")
    @RequestMapping(value = "/getStuStatisticsByTeaId", method = RequestMethod.POST)
    public void getStuStatisticsByTeaId(String jsonStr,HttpServletRequest request, HttpServletResponse response) throws IOException {

        //转换json格式工具
        ObjectMapper mapper = new ObjectMapper();
        //存放结果值
        Map<String,Object> resultMap = new HashMap<String,Object>();
        try {
            //获取参数值
            Map<String,Object> paramMap = mapper.readValue(jsonStr, resultMap.getClass());
            //学生统计
            Map<String,Object> stuStatisticsMap = teaStatisticsActionImpl.getStuStatisticsByTeaId(paramMap,request);
            resultMap.put("stuStatisticsMap",stuStatisticsMap);
            resultMap.put("result", "success");
            resultMap.put("failMsg", "");
        } catch (Exception e) {
            resultMap.put("result", "fail");
            resultMap.put("failMsg", "跳转页面失败");
            e.printStackTrace();
            logger.error(" 获取学生统计出现异常", e);

        } finally {
            logger.info("获取学生统计的返回结果为："+resultMap);
            PubMethods.postMed(response,mapper,resultMap);
        }
    }

    /**
     * 学生统计--详情
     * param:学生id，课程id
     * @return 点击详情显示每门课程的实验时长、课程成绩分数、当前课程排名。每个章节的课程成绩（平时成绩）、班级平均成绩、实验时长、该实验章节平均时长。
     * @throws IOException
     */
    @SuppressWarnings("unchecked")
    @RequestMapping(value = "/getStuStatisticsDetailByStuId", method = RequestMethod.POST)
    public void getStuStatisticsDetailByStuId(String jsonStr,HttpServletRequest request, HttpServletResponse response) throws IOException {

        //转换json格式工具
        ObjectMapper mapper = new ObjectMapper();
        //存放结果值
        Map<String,Object> resultMap = new HashMap<String,Object>();
        try {
            //获取参数值
            Map<String,Object> paramMap = mapper.readValue(jsonStr, resultMap.getClass());
            //学生统计
            Map<String,Object> stuStatisticsDetailMap = teaStatisticsActionImpl.getStuStatisticsDetailByStuId(paramMap,request);
            resultMap.put("stuStatisticsDetailMap",stuStatisticsDetailMap);
            resultMap.put("result", "success");
            resultMap.put("failMsg", "");
        } catch (Exception e) {
            resultMap.put("result", "fail");
            resultMap.put("failMsg", "跳转页面失败");
            e.printStackTrace();
            logger.error(" 获取学生统计--详情出现异常", e);

        } finally {
            logger.info("获取学生统计--详情的返回结果为："+resultMap);
            PubMethods.postMed(response,mapper,resultMap);
        }
    }

    /**
     * 教师端--学生学习报告列表
     * param:学生姓名，课程id、班级id----搜素查询
     * param:pageNow、pageSize
     * @return 学生列表
     * @throws IOException
     */
    @SuppressWarnings("unchecked")
    @RequestMapping(value = "/getAllStuReportByTeaId", method = RequestMethod.POST)
    public void getAllStuReportByTeaId(String jsonStr,HttpServletRequest request, HttpServletResponse response) throws IOException {

        //转换json格式工具
        ObjectMapper mapper = new ObjectMapper();
        //存放结果值
        Map<String,Object> resultMap = new HashMap<String,Object>();
        try {
            //获取参数值
            Map<String,Object> paramMap = mapper.readValue(jsonStr, resultMap.getClass());
            //学生列表
            Map<String,Object> stuMap = teaStatisticsActionImpl.getAllStuReportByTeaId(paramMap,request);
            resultMap.put("stuMap",stuMap);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(" 学生学习报告列表出现异常", e);

        } finally {
            logger.info("学生学习报告列表的返回结果为："+resultMap);
            PubMethods.postMed(response,mapper,resultMap);
        }
    }

    /**
     * 在线预览学生报告
     * param:学生id
     * @return 统计学生的实验总时间、提问问题数、回答问题数、提交作业数、实验报告数。实验笔记数、平时成绩、考试成绩、总成绩、学生姓名、专业、院系、课程名
     * @throws IOException
     */
    @SuppressWarnings("unchecked")
    @RequestMapping(value = "/getStuReportDetailByStuId", method = RequestMethod.POST)
    public void getStuReportDetailByStuId(String jsonStr,HttpServletRequest request, HttpServletResponse response) throws IOException {

        //转换json格式工具
        ObjectMapper mapper = new ObjectMapper();
        //存放结果值
        Map<String,Object> resultMap = new HashMap<String,Object>();
        try {
            //获取参数值
            Map<String,Object> paramMap = mapper.readValue(jsonStr, resultMap.getClass());
            //学生统计
            Map<String,Object> stuStatisticsMap = teaStatisticsActionImpl.getStuReportDetailByStuId(paramMap,request);
            resultMap.put("stuStatisticsMap",stuStatisticsMap);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(" 获取在线预览学生报告出现异常", e);

        } finally {
            logger.info("获取在线预览学生报告的返回结果为："+resultMap);
            PubMethods.postMed(response,mapper,resultMap);
        }
    }

    /**
     * 保存学生报告html
     * param:学生id,reportHtml
     * @return
     * @throws IOException
     */
    @SuppressWarnings("unchecked")
    @RequestMapping(value = "/saveReportHtmlByStuId", method = RequestMethod.POST)
    public void saveReportHtmlByStuId(String jsonStr,HttpServletRequest request, HttpServletResponse response) throws IOException {

        //转换json格式工具
        ObjectMapper mapper = new ObjectMapper();
        //存放结果值
        Map<String,Object> resultMap = new HashMap<String,Object>();
        try {
            String fialMsg = "";
            //获取参数值
            Map<String,Object> paramMap = mapper.readValue(jsonStr, resultMap.getClass());
            //学生统计
            Map<String,Object> stuStatisticsMap = teaStatisticsActionImpl.saveReportHtmlByStuId(paramMap,request);
            String result = Boolean.parseBoolean(String.valueOf(stuStatisticsMap.get("flag")))==true?"success":"fail";
            resultMap.put("result",result);
            if(result.equals("success")){
                resultMap.put("failMsg","");
            }else{
                resultMap.put("failMsg","操作失败");
            }
            resultMap.put("reportFileName",stuStatisticsMap.get("reportFileName"));
            resultMap.put("path",stuStatisticsMap.get("path"));

        } catch (Exception e) {
            resultMap.put("result","fail");
            resultMap.put("failMsg","操作失败");
            e.printStackTrace();
            logger.error(" 保存学生报告html出现异常", e);

        } finally {
            logger.info("保存学生报告html的返回结果为："+resultMap);
            PubMethods.postMed(response,mapper,resultMap);
        }
    }
}
